// pages/myroom/myroom.js
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
var wxAuthFail = false;
var uid='';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    util:util  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var tc = app.get_tc();
    that.setData({
      tc: tc,
    })
    var inviteuid = options.inviteuid ? options.inviteuid : '';
    that.getLoginInfo();
    var title = ''
    if (util.orgid == 20510) {
      title = tc == 1 ? '申請加入會員':'申请加入会员';

    }
    if (util.orgid == 18535) {
      title = '申请加入家家公寓';

    }
    wx.setNavigationBarTitle({
      title: title,
    })
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
      // that.resetNavColor();
    });
    wx.getSystemInfo({
      success: function (res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screen_h: res.windowHeight,
        });
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  ////表单提交触发事件
  formSubmit: function (e) {
    app.saveFormId(e.detail.formId);
    

    var that = this;


    console.log(e);

    var uname = e.detail.value.username;
    var phone = e.detail.value.mobile;
    if (util.orgid == 18535) {
      var message = "【申请加入家家公寓】房间信息： " + e.detail.value.desc;

    }
    if (util.orgid == 20510) {
      var message = that.data.tc == 1 ? '申請加入會員' : '' + e.detail.value.desc;

    }

    if (uname == "") {
      var contentname = this.data.tc == 1 ? '姓名不能為空' : '姓名不能为空';
      wx.showModal({
        title: '提示',
        content: contentname,
        showCancel: false,
      })
      return;
    } else if (phone == "") {
      var contentmobile = this.data.tc == 1 ? '手機號不能為空' : '手机号不能为空';
      wx.showModal({
        title: '提示',
        content: contentmobile,
        showCancel: false,
      })
      return;
    } 
    var privatekey = utilMd5.hexMD5(that.data.uid + 'postliuyan' + that.data.token);
    util.api({
      method: "POST",
      data: {
        ac: "postliuyan",
        uid: that.data.uid,
        uname: uname,
        phone: phone,
        message: message,
        orgid: util.orgid,
        privatekey: privatekey
      },
      success: function (responseData) {
        console.log(responseData);
        if (responseData.data.res == true) {
          var contentname = this.data.tc == 1 ? '資料已提交，請耐心等待審核' : '资料已提交，请耐心等待审核';
          that.setData({
            response: contentname
          })
          app.dialogToast(contentname, "success", function () {
            // wx.reLaunch({ url: '/pages/liuyan-center/liuyan-center' });
          })
        } else {
          wx.showToast({
            title: '提交失败',
            icon: 'success'
          })
        }

      }
    });
  },

  //重新读取 登录用户信息
  reGetUserInfo: function (e) {
    var that = this;
    console.log("reGetUserInfo: ", e);
    if (e == 1) {
      that.getLoginInfo();
    }
  },
  getLoginInfo: function () {
    var that = this;
    app.LoginUserInfo(function (res) {
      if (res && res != -1) {

        //更新数据
        wxAuthFail = false;
        uid = res.uid;
        that.setData({
          userInfo:res,
          uid: res.uid,
          token: res.token,
          loginUname: res.uname,
          openid: res.wxsmall_openid
        })

      } else {
        wxAuthFail = true;
      }
    }, '')
  },
})